package com.gzsxy.excel.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author
 * @Description:
 * @date 2022/1/4 14:43
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    @ApiModelProperty("状态码")
    private int code;

    @Getter
    @Setter
    @ApiModelProperty("错误信息")
    private String msg;

    @Getter
    @Setter
    @ApiModelProperty("返回数据")
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, ErrorCode.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ErrorCode.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ErrorCode.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, ErrorCode.FAIL, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, ErrorCode.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ErrorCode.FAIL, null);
    }

    public static <T> R<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ErrorCode.FAIL, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
